﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Web.Http;
using Newtonsoft.Json;
using PagedList;
using Sovigaz.DataProvider;
using System.Linq.Dynamic;
using Utilities;

namespace Sovigaz.Organization
{
    public class OrganizationAPIController : ApiController
    {
        private readonly SovigazDbContext _context = new SovigazDbContext();

        public OrganizationModel Get(int? pageNumber, string orderBy, string orderDirection)
        {
            try
            {
                var organizations = _context.Organizations
                            .Where(p => p.Status != "D")
                            .OrderBy(String.Format("{0} {1}", orderBy, orderDirection).Trim())
                            .ToPagedList(pageNumber ?? 1, GenericRepository.DefaultPageSize);

                //var orgModel = new OrganizationModel
                //{
                //    TotalItemCount = organizations.TotalItemCount,
                //    TotalItemOnPage = organizations.Count,
                //    PageCount = organizations.PageCount,
                //    PageNumber = organizations.PageNumber,
                //    Organizations = organizations.ToList()
                //};

                //return orgModel;
                return null;
            }
            catch
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu."
                    , HttpStatusCode.BadRequest);
            }
        }

        public OrganizationModel.Organization Get(string id)
        {
            try
            {
                var organizations = _context.Organizations.FirstOrDefault(p => p.OrganizationID == id);
                return organizations;
            }
            catch
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu."
                    , HttpStatusCode.BadRequest);
            }
        }

        public OrganizationModel Delete(string ids, int? pageNumber, string orderBy, string orderDirection)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                foreach (var id in idArr)
                {
                    var org = _context.Organizations.FirstOrDefault(p => p.OrganizationID == id);
                    if (org != null)
                        org.Status = "D";
                }
                _context.SaveChanges();

                return Get(pageNumber, orderBy, orderDirection);
            }
            catch
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu."
                    , HttpStatusCode.BadRequest);
            }
        }

        public void Put(string id, string data)
        {
            var organization = JsonConvert.DeserializeObject<OrganizationModel.Organization>(data);

            if (id != organization.OrganizationID)
                throw new Exception("Mã phòng ban không đúng.");

            try
            {
                var currentOrg = Get(organization.OrganizationID);
                //if (currentOrg.Tstamp != organization.Tstamp)
                //    throw new Exception("Dữ liệu đã bị cập nhật.");

                currentOrg.OrganizationID = organization.OrganizationID;
                currentOrg.Name = organization.Name;
                currentOrg.Phone = organization.Phone;
                currentOrg.Fax = organization.Fax;
                currentOrg.Status = organization.Status;

                _context.SaveChanges();
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch
            {
                throw ExceptionHelper.CreateHttpResponseException("Không tìm thấy dữ liệu."
                    , HttpStatusCode.BadRequest);
            }
        }
    }
}
